package com.teame.readbaby;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.teame.readbaby.BaseActivity.DataCallback;
import com.teame.readbaby.TopicProductlistActivity.MyAdapter;

import com.teame.readbaby.parser.TopicProductListParser;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.Logger;
import com.teame.readbaby.vo.TopicListInfo;
import com.teame.readbaby.vo.TopicProductListInfo;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ProductListActivity extends BaseActivity {

	private String id;
	private ListView categoryList;
	private String TAG = "ProductListActivity";
	private List<Map<String, Object>> data;
	private List<TopicProductListInfo> list;
	private GridView nineGv;

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void findViewById() {
		nineGv = (GridView) this.findViewById(R.id.nineGv);

	}

	@Override
	protected void loadViewLayout() {
		id = getIntent().getStringExtra("id");
		Logger.i(TAG, "get id ==" + id);
		this.setContentView(R.layout.productlist_activity);// 需要自己定义布局了呜呜呜

	}

	@Override
	protected void processLogic() {
		String url = ConstantValue.HOST + ConstantValue.PRODUCTLIST;
		// TODO Auto-generated method stub
		// Map<String, String> params=new HashMap<String, String>();
		// params.put("id", id);
		// getDataFromServer(url, params, baseParser, callBack);
		this.getDataFromServer(url, null, new TopicProductListParser(),
				new DataCallback<List<TopicProductListInfo>>() {

					@Override
					public void processData(
							List<TopicProductListInfo> paramObject,
							boolean paramBoolean) {
//						data = new ArrayList<Map<String, Object>>();
//						for (TopicProductListInfo info : paramObject) {
//							Map<String, Object> map = new HashMap<String, Object>();
//							map.put("商品ID", info.getId()+"");
//							map.put("商品名称", info.getName());
//							map.put("图片URL", info.getPic());
//							map.put("市场价", info.getMarketprice());
//							map.put("会员价", info.getPrice()+"");
//							data.add(map);
//							Logger.i(TAG, info.toString());
						list=paramObject;
						nineGv.setAdapter(new MyAdapter());
						}

					
				});

	}
	
	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder=null;
			if(convertView==null)
			{
				convertView=View.inflate(context, R.layout.product_child_listitem, null);
				holder=new ViewHolder();
				holder.tv_title= (TextView) convertView.findViewById(R.id.tv_title);
				holder.tv_price=(TextView) convertView.findViewById(R.id.tv_price);
				holder.iv_image=(ImageView) convertView.findViewById(R.id.iv_image);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			TopicProductListInfo info=list.get(position);
			holder.tv_title.setText(info.getName());
			holder.tv_price.setText(" "+info.getPrice());
			//图片获取
			String url=info.getPic();
			return convertView;
		}
	
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onClickEvent(View paramView) {
		// TODO Auto-generated method stub

	}
	private static class ViewHolder {
		
		TextView tv_price;
		TextView tv_title;
		ImageView iv_image;
	}

}
